package com.shanghaionstar.servicepackage.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.more.entity.PackageInfo;
import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OnstarPkgListAdapter extends CommonAdapter<PackageInfo> {
    private Context mContext;

    private List<PackageInfo> mPackageList;
    SimpleDateFormat df;
    public int OnstarTotalremain = 0;

    public OnstarPkgListAdapter(Context context) {
        super(null, context);
        mContext = context;
        df = new SimpleDateFormat("yyyy-MM-dd");
    }

    public void addAll(List<PackageInfo> datas){
        if (mPackageList == null)
            mPackageList = new ArrayList<>();
        mPackageList.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mPackageList == null ? 0:mPackageList.size();
    }

    @Override
    public Object getItem(int position) {
        return mPackageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    /**
     * 字符串转化为date
     *
     * @return java.util.Date
     */

    private Date StringToate(String s) {
        Date date = null;
        if (!TextUtils.isEmpty(s)) {
            if (s.contains("+")) {
                s = s.substring(0, s.indexOf("+"));
            }
            try {
                date = df.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    private String FormatPackageDate(String s) {
        if (!TextUtils.isEmpty(s)) {
            return df.format(StringToate(s));
        } else {
            return "";
        }
    }

    @Override
    public void getView(int position, CommonViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder) viewHolder;
        PackageInfo bean = mPackageList.get(position);
        int Totalremain = 0;
        //if (null != bean && !StatusTypeEnum.HISTORY.equals(bean.getStatus().value())) {
        holder.packageName.setText(TextUtils.isEmpty(bean.getPackageName()) ? mContext.getString(R.string.package_list) : bean.getPackageName());
        holder.duration.setText(TextUtils.isEmpty(bean.getPackageName()) ? "" : bean.getDuration().substring(0, 2));
        // 判断list 中的数据是几个显示不同的图片
        if (position == 0) {
            if (position == 0 && mPackageList.size() == 1) {// 如果数据是第一个并且只有一个就显示一个蓝色的圆点
                holder.noStarted.setBackground(mContext.getResources().getDrawable(R.drawable.service_yuan));
            } else {
                holder.noStarted.setBackground(mContext.getResources().getDrawable(R.drawable.service_nu_line));
            }
        } else if (position == mPackageList.size() - 1) {// 如果数据是最后一个就显示一个绿色的圆点
            holder.noStarted.setBackground(mContext.getResources().getDrawable(R.drawable.service_nyuan));
        } else {
            // list中间的数据
            holder.noStarted.setBackground(mContext.getResources().getDrawable(R.drawable.service_nu));
        }
        if (!TextUtils.isEmpty(bean.getStartDate())) {
            holder.startDay.setText(FormatPackageDate(bean.getStartDate()));
            holder.endDay.setText(FormatPackageDate(bean.getEndDate()));
            Date startday = StringToate(bean.getStartDate());
            LogManager.getInstance().i("startday.getTime() " + startday.getTime()
                    + ",System.currentTimeMillis() " + System.currentTimeMillis());
            if (startday.getTime() > System.currentTimeMillis()) {// 开始时间>当前时间  =未开始 holder.noStarted.setVisibility(View.VISIBLE);
                int uremain = 0;
                if (!TextUtils.isEmpty(bean.getEndDate())) {
                    Date endDay = StringToate(bean.getEndDate());
                    uremain = (int) ((endDay.getTime() - System.currentTimeMillis()) / (24 * 60 * 60 * 1000) + 1);
                    Totalremain = uremain;
                }
                holder.listLayout.setFocusable(true);
            } else {// 计算并显示剩余天数
                holder.packageName.setTextColor(mContext.getResources().getColor(R.color.profile_font_color));
                holder.startDay.setTextColor(mContext.getResources().getColor(R.color.profile_tr_font_color));
                holder.endDay.setTextColor(mContext.getResources().getColor(R.color.profile_tr_font_color));
                holder.startDayTxt.setTextColor(mContext.getResources().getColor(R.color.profile_tr_font_color));
                holder.endDayTxt.setTextColor(mContext.getResources().getColor(R.color.profile_tr_font_color));
                holder.listLayout.setEnabled(true);

                int remain = 0;
                if (0 != bean.getRemainingDay()) {
                    remain = bean.getRemainingDay();
                    Totalremain = remain;
                } else if (!TextUtils.isEmpty(bean.getEndDate())) {
                    Date endDay = StringToate(bean.getEndDate());
                    remain = (int) ((endDay.getTime() - System.currentTimeMillis()) / (24 * 60 * 60 * 1000) + 1);// 结束日期-今日凌晨=剩余日期；因为currentTimeMillis取的是当前时间  多减了n（0到23）个小时 所以算出来的剩余天数会少一天   所以加一
                    Totalremain = remain;

                }
            }

        }
        //}
        OnstarTotalremain = OnstarTotalremain + Totalremain;
    }

    @Override
    public int layoutId() {
        return R.layout.onstar_payme_package_onstar_list_item;
    }

    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    private class ViewHolder extends CommonViewHolder {
        @FindViewById(value = R.id.package_name)
        TextView packageName;
        @FindViewById(value = R.id.start_day)
        TextView startDay;
        @FindViewById(value = R.id.end_day)
        TextView endDay;
        @FindViewById(value = R.id.start_day_text)
        TextView startDayTxt;
        @FindViewById(value = R.id.end_day_text)
        TextView endDayTxt;
        @FindViewById(value = R.id.package_no_started)
        CustomImageView noStarted;
        @FindViewById(value = R.id.layout_payme_package_list)
        RelativeLayout listLayout;
        @FindViewById
        TextView duration;
    }
}